package com.k.base.jdk.anno.demo1;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author k
 */
public class Test {
    public static void main(String[] args) {

        List<User> list = new ArrayList<>();
        for(int i=0;i<5;i++){
            User u = new User("1","ljw",2);
            list.add(u);
        }
        fieldAnno(list);
        methodAnno(list);
        classAnno(User.class);
    }

    /**
     * 类 注解
     * @param clazz class
     */
    public static void classAnno(Class clazz){
        Annotation[] annArr = clazz.getAnnotations();
        for(Annotation ann:annArr){
            if(ann instanceof ClassAnno){
                System.out.println("=== zhao dao @ClassAnno");
                ClassAnno classAnno = (ClassAnno) ann;
                System.out.println(classAnno.value());
            }
        }
    }

    /**
     * 获取字段注解
     * @param list obj
     */
    public static void fieldAnno(List<User> list){
        if(list != null){
            User user = list.get(0);
            Class clazz = user.getClass();
            Field[] fieldArr = clazz.getDeclaredFields();
            System.out.println("all属性 不包含父类属性:"+fieldArr.length+" :"+Arrays.toString(fieldArr));
            Field[] fields2 =clazz.getFields();
            System.out.println("public属性 包含父类属性:"+fieldArr.length+" :"+Arrays.toString(fields2));
            for(Field field:fieldArr){
                System.out.println("Field info: "+field.getName());
                boolean isExcelAnno = field.isAnnotationPresent(ExcelAnno.class);
                if(isExcelAnno){
                    ExcelAnno eanno = field.getAnnotation(ExcelAnno.class);
                    String name = eanno.name();
                    int order = eanno.order();
                    System.out.println("ExportInfo:"+"field name:"+field.getName()+"Anno info: "+"name:"+name+" order:"+order);
                }
                System.out.println("==========");
            }
        }
    }

    /**
     * 获取方法注解
     * @param list obj
     */
    public static void methodAnno(List<User> list) {
        // 获取所有的方法
        User user = list.get(0);
        Class clazz = user.getClass();
        Method[] methods = clazz.getDeclaredMethods();
        System.out.println("不包含父类方法："+methods.length+":"+Arrays.toString(methods));
        Method[] methods1 = clazz.getMethods();
        System.out.println("包含父类方法："+methods1.length+":"+Arrays.toString(methods1));
        for (Method method : methods) {
            boolean isAnno = method.isAnnotationPresent(MethodAnno.class);
            if (isAnno) {
                // 获取自定义注解对象
                MethodAnno methodAnno = method.getAnnotation(MethodAnno.class);
                // 根据对象获取注解值
                String name = methodAnno.name();
                System.out.println("method anno value:"+name);
            }
        }
    }
}
